﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models.Utils
{
    public static class FileUtils
    {
        public static string CheckFileExists(HttpPostedFileBase file, string path, string newFileName = null)
        {
            string tmpFileName = "";

            if (String.IsNullOrEmpty(newFileName))
            {
                tmpFileName = System.IO.Path.GetFileName(file.FileName);
            }
            else
            {
                tmpFileName = newFileName;
            }
            tmpFileName = tmpFileName.Replace(' ', '_');

            string name = System.IO.Path.GetFileNameWithoutExtension(tmpFileName);
            string extention = System.IO.Path.GetExtension(tmpFileName);

            int fileCount = System.IO.Directory.GetFiles(path, name + "*" + extention, System.IO.SearchOption.TopDirectoryOnly).Length;
            if (fileCount > 0)
            {
                tmpFileName = string.Format("{0}({1}){2}", name, fileCount, extention);
            }
            return tmpFileName;

        }

    }
}